#include <iostream>
#include <stdio.h>

using namespace std;

int l[100000];

unsigned int find1(unsigned int a, unsigned int b);

int main(){
    int n;
    cin >> n;
    unsigned int a;
    unsigned int b;
    for(int i = 0; i< n; i++){
        cin >> a >> b;
        l[i] = find1(a,b);
    }
    for(int i = 0; i< n; i++){
        printf("%d\n",l[i]);
    }
}


unsigned int find1(unsigned int a, unsigned int b){
    return max(a%b,b%a);
}